package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.audi.R;
import com.audi.activity.HomeControlActivity;
import com.audi.dto.PlaceItem;
import com.audi.view.DeviceUpdatePhoto;

public class ListPlaceCardAdapter extends BaseAdapter {
	private Activity mActivity;
	private DeviceUpdatePhoto mDeviceUpdatePhoto;
	private ArrayList<PlaceItem> mPlaceCardList;
	private PopupMenu mPopMenu;

	public ListPlaceCardAdapter(Activity activity,
			ArrayList<PlaceItem> placecradList,
			DeviceUpdatePhoto mDeviceUpdatePhoto) {
		mActivity = activity;
		mPlaceCardList = placecradList;
		this.mDeviceUpdatePhoto = mDeviceUpdatePhoto;
	}

	public void swapItems(ArrayList<PlaceItem> placeItems) {
		this.mPlaceCardList = placeItems;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mPlaceCardList.size();
	}

	@Override
	public PlaceItem getItem(int position) {
		return mPlaceCardList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup view) {
		PlaceItem item = mPlaceCardList.get(position);
		ViewHolder viewholder = null;

		// khoi tao cho viewholder
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = LayoutInflater.from(mActivity).inflate(
					R.layout.row_place_card, null);
			viewholder.imageRoom = (ImageView) convertView
					.findViewById(R.id.img_room_tab_place);
			viewholder.nameRoom = (TextView) convertView
					.findViewById(R.id.tv_name_room_tab_place);
			viewholder.typePlace = (TextView) convertView
					.findViewById(R.id.tv_capability_tab_place);
			viewholder.imageMenu = (ImageView) convertView
					.findViewById(R.id.image_menu_tab_place);
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		// set data for listView
		if (TextUtils.isEmpty(item.getPlaceImage())) {
			viewholder.imageRoom.setImageResource(R.drawable.icon_plug_on_off);
		} else {
			BitmapDrawable bm = new BitmapDrawable(mActivity.getResources(),
					item.getPlaceImage());
			viewholder.imageRoom.setImageDrawable(bm);
		}

		viewholder.nameRoom.setText(item.getPlaceName());
		System.out.println(item.getPlaceName());
		String[] stringArray = mActivity.getResources().getStringArray(
				R.array.list_privacy_text);
		try {
			viewholder.typePlace
					.setText(stringArray[item.getCapabilityType() - 1]);
		} catch (Exception e) {
		}

		onclick(viewholder, position);

		return convertView;
	}

	public void onclick(final ViewHolder viewholder, final int position) {

		viewholder.imageMenu.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mPopMenu = new PopupMenu(mActivity, viewholder.imageMenu);
				mPopMenu.getMenuInflater().inflate(R.menu.menu_list_place_card,
						mPopMenu.getMenu());
				mPopMenu.show();
				mPopMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

					@Override
					public boolean onMenuItemClick(MenuItem index) {
						if (index.getItemId() == R.id.placeMenu_editName) {
							PlaceItem placeItem = new PlaceItem();
							placeItem.setPlaceId(mPlaceCardList.get(position)
									.getPlaceId());
							placeItem.setPlaceName(mPlaceCardList.get(position)
									.getPlaceName());
							((HomeControlActivity) mActivity)
									.renamePlaceFuntion(placeItem);
						}
						if (index.getItemId() == R.id.placeMenu_editPrivacy) {
							((HomeControlActivity) mActivity)
									.changeCapabilityOfPlace(mPlaceCardList
											.get(position));
						}
						if (index.getItemId() == R.id.placeMenu_editPhoto) {
							mDeviceUpdatePhoto.updatePhoto();
						}
						if (index.getItemId() == R.id.placeMenu_delete) {
							((HomeControlActivity) mActivity)
									.deletePlace(mPlaceCardList.get(position));
						}
						return true;
					}
				});

			}
		});

	}

	static class ViewHolder {
		private ImageView imageRoom;
		private TextView nameRoom;
		private TextView typePlace;
		private ImageView imageMenu;

	}

}
